package com.qyhl.webtv.module_user.login.modifypwd;


import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.base.BaseModel;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.user.UserBean;
import com.qyhl.webtv.module_user.app.UserApplication;
import com.qyhl.webtv.module_user.common.UserUrl;
import com.qyhl.webtv.module_user.util.LoginUtils;

import java.util.Map;

public class NewModifyPresenterModel extends BaseModel implements NewModifyContract.NewModifyPresenterModel {
    private NewModifyContract.NewModifyView view;

    public NewModifyPresenterModel(NewModifyContract.NewModifyView view) {
        super();
        this.view = view;
    }

    @Override
    public void getCode(String phone) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.GET_USER_GETCODE);

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + CommonUtils.getInstance().getSite() + "&type=2&telphone=" + phone))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            view.getCodeError("获取验证码失败!");
                        }

                        @Override
                        public void onSuccess(String s) {
                            view.getCodeSuccess();
                        }
                    });
        } else {
            view.getCodeError("请检查网络...");
        }
    }

    @Override
    public void goConfirm(String phone, String password, String code) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.GET_MODIFY_PWD);

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + CommonUtils.getInstance().getSite() + "&password=" + password +
                                    "&username=" + phone + "&captcha=" + code))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<UserBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.modifyError(LoginUtils.getMessage(e.getCode()));
                        }

                        @Override
                        public void onSuccess(UserBean userBean) {
                            view.modifySuccess();
                        }
                    });

        } else {
            view.modifyError("请检查网络...");
        }
    }
}
